import request from '@/utils/request'

// 获取部门组织请求
export function departmentRequest() {
  return request({
    url: '/company/department',
    method: 'GET'

  })
}

// 获取部门负责人
export function getUserSimpleRequest() {
  return request({
    url: '/sys/user/simple',
    method: 'GET'

  })
}

// 添加子部门
export function addDepartmentRequest(data) {
  return request({
    url: '/company/department',
    method: 'POST',
    data
  })
}

// 删除部门
export function deleteDepartmentRequest(id) {
  return request({
    url: `/company/department/${id}`,
    method: 'DELETE'
  })
}

// 编辑部门
export function editDepartmentRequest(data) {
  return request({
    url: `/company/department/${data.id}`,
    method: 'PUT',
    data
  })
}

// 获取部门详情
export function getDepartmentDetailRequest(id) {
  return request({
    url: `/company/department/${id}`,
    method: 'GET'
  })
}

// 递归函数,组织部门平铺结构转化树形结构
export function transformListToTree(list, rootId) {
  const arr = []
  list.forEach((item) => {
    if (item.pid === rootId) {
      // item.children = transformListToTree(list, item.id)
      const tmp = transformListToTree(list, item.id)
      if (tmp.length > 0) {
        item.children = tmp
      }
      arr.push(item)
    }
  })
  return arr
}
